package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.MallMember;
import com.sc.nft.entity.MallMemberEquity;
import com.sc.nft.entity.dto.*;
import com.sc.nft.mapper.MallMemberEquityMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

/**
 * @Author：LiGen
 * @Package：com.sc.nft.dao
 * @Project：sc_metago
 * @name：MallMemberEquityDao
 * @Date：2024/4/2 19:29
 * @Filename：MallMemberEquityDao
 */
@Slf4j
@Repository
public class MallMemberEquityDao extends ServiceImpl<MallMemberEquityMapper, MallMemberEquity> {

    /**
     * 获取商城会员权益
     *
     * @param mallMemberId               商城会员Id
     * @param mallMemberEquityTemplateId 商城会员权益模版Id
     * @return
     */
    public MallMemberEquity get(Long mallMemberId, Long mallMemberEquityTemplateId) {
        LambdaQueryWrapper<MallMemberEquity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallMemberEquity::getMallMemberId, mallMemberId);
        wrapper.eq(MallMemberEquity::getMallMemberEquityTemplateId, mallMemberEquityTemplateId);
        wrapper.orderByDesc(MallMemberEquity::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    /**
     * 移除商城会员权益
     *
     * @param mallMemberId               商城会员Id
     * @param mallMemberEquityTemplateId 商城会员权益模版Id
     * @return
     */
    public void remove(Long mallMemberId, Long mallMemberEquityTemplateId) {
        LambdaQueryWrapper<MallMemberEquity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallMemberEquity::getMallMemberId, mallMemberId);
        wrapper.eq(MallMemberEquity::getMallMemberEquityTemplateId, mallMemberEquityTemplateId);
        remove(wrapper);
    }

    /**
     * 获取商城会员权益列表
     *
     * @param mallMemberId 商城会员Id
     * @return
     */
    public List<MallMemberEquity> getByMallMemberId(Long mallMemberId) {
        LambdaQueryWrapper<MallMemberEquity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallMemberEquity::getMallMemberId, mallMemberId);
        return list(wrapper);
    }

    /**
     * 获取商城会员权益列表
     *
     * @param mallMemberEquityTemplateId 商城权益模版Id
     * @return
     */
    public List<MallMemberEquity> getByMallMemberEquityTemplateId(Long mallMemberEquityTemplateId) {
        LambdaQueryWrapper<MallMemberEquity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallMemberEquity::getMallMemberEquityTemplateId, mallMemberEquityTemplateId);
        return list(wrapper);
    }

    /**
     * 获取商城会员权益列表(app)
     *
     * @param mallMemberId
     * @return
     */
    public List<MallMemberEquityPageDTO> getMallMemberEquityPageDTO(Long mallMemberId) {
        return baseMapper.getMallMemberEquityPageDTO(mallMemberId);
    }

    /**
     * 获取优先购商品列表(app)
     *
     * @param mallMemberId
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<MallGrabCommoditiesFirstPageDTO> getMallGrabCommoditiesFirstPageDTO(Long mallMemberId, Integer pageNo, Integer pageSize) {
        return baseMapper.getMallGrabCommoditiesFirstPageDTO(new Page(pageNo, pageSize), mallMemberId);
    }

    /**
     * 获取商城会员权益(后台)
     *
     * @param memberId
     * @return
     */
    public List<AdminMallMemberEquityDTO> getAdminMallMemberEquityDTO(Long memberId) {
        return baseMapper.getAdminMallMemberEquityDTO(memberId);
    }

    /**
     * 获取会员权益模版优惠券（admin）
     *
     * @param memberId
     * @param mallMemberEquityTemplateId
     * @return
     */
    public List<AdminMallMemberCouponsDTO> getAdminMallMemberCouponsDTO(Long memberId, Long mallMemberEquityTemplateId) {
        return baseMapper.getAdminMallMemberCouponsDTO(memberId, mallMemberEquityTemplateId);
    }

    /**
     * 获取会员权益模版商品（admin）
     *
     * @param memberId
     * @param mallMemberEquityTemplateId
     * @return
     */
    public List<AdminMallMemberProductDTO> getAdminMallMemberProductDTO(Long memberId, Long mallMemberEquityTemplateId) {
        return baseMapper.getAdminMallMemberProductDTO(memberId, mallMemberEquityTemplateId);
    }

    /**
     * 获取可添加至会员权益模版商品列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<AdminMallMemberProductDTO> getCanAddAdminMallMemberProductDTO(String productName,Integer pageNo, Integer pageSize) {
        return baseMapper.getCanAddAdminMallMemberProductDTO(new Page(pageNo, pageSize),productName);
    }

    /**
     * 获取可添加至会员权益模版商品列表
     *
     * @param activityId
     * @return
     */
    public List<AdminMallMemberProductSpecsDTO> getCanAddAdminMallMemberProductSpecsDTO(Long activityId){
        return baseMapper.getCanAddAdminMallMemberProductSpecsDTO(activityId);
    }
}
